package com.unitd.frame.comm.utils;

import java.util.Formatter;

/**
 * @desc 字节 16进制字串转换工具类
 * @filename Byte2Hex.java
 * @version 1.0
 * @copyright www.pactera.com.cn
 * @author Hudan
 * @date 2016/10/27
 */
public class ByteUtils {

	/**
	 * @desc 字节转换为 16 进制字符串
	 * @param b 字节
	 * @return
	 */
	public static String byte2Hex(byte b) {
		String hex = Integer.toHexString(b);
		if (hex.length() > 2) {
			hex = hex.substring(hex.length() - 2);
		}
		while (hex.length() < 2) {
			hex = "0" + hex;
		}
		return hex;
	}

	/**
	 * @desc 字节数组转换为 16 进制字符串
	 * @param bytes 字节数组
	 * @return
	 */
	public static String byte2Hex(byte[] bytes) {
		Formatter formatter = new Formatter();
		for (byte b : bytes) {
			formatter.format("%02x", b);
		}
		String hash = formatter.toString();
		formatter.close();
		return hash;
	}
}
